package com.cci.kangdao.service.impl;


import com.cci.kangdao.dao.ECJVServiceBookDao;
import com.cci.kangdao.service.ECJVServiceBookService;
import com.cci.kangdao.utilTool.*;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author linjiankun
 * @date 2022/6/10 9:47
 */
@Service("ECJVServiceBookService")
public class ECJVServiceBookServiceImpl implements ECJVServiceBookService {

    private Logger log = Logger.getLogger(this.getClass().getName());

    @Resource
    private ECJVServiceBookDao ecjvServiceBookDao;

    /**
     * 根据机型和服务手册类型获取ECJV服务手册目录列表
     * @param map
     * @return
     */
    public ModelAndView getECJVServiceBookCatalogue(Map map) {
        HttpServletRequest request = (HttpServletRequest) map.get("request");
        HttpServletResponse response = (HttpServletResponse) map.get("response");
        try {
            // 1.数据校验
            //ecjv机型字典项的DInt值
            int ecjvEngineTypeDInt = MapUtils.getIntValue(map,"ecjvEngineTypeDInt",-1);
            //ecjv服务手册类型列表字典项的DInt值（维护操作手册 维修手册 故障诊断手册使用该字段查询数据,电路图和工具清单没有目录）
            /*1	维护操作手册
            2	维修手册
            3	电路图
            4	故障诊断手册
            5	工具清单*/
            int ecjvServiceBookConfigDInt = MapUtils.getIntValue(map,"ecjvServiceBookConfigDInt",-1);
            if(ecjvEngineTypeDInt == -1 || (ecjvServiceBookConfigDInt != 1 && ecjvServiceBookConfigDInt != 2 && ecjvServiceBookConfigDInt != 4)){
                ParameterTool.writeErrorResponse(response,-5,"缺少必要参数或所传参数非枚举值！");
                return null;
            }
            //ECJV_Handbook_T表映射值是（11：AMT故障诊断手册     12：AMT维修手册    13：维护操作手册）
            if(ecjvServiceBookConfigDInt == 1){   //1	维护操作手册   =====    13：维护操作手册
                ecjvServiceBookConfigDInt = 13;
            }
            if(ecjvServiceBookConfigDInt == 2){   //2	维修手册   =====    12：AMT维修手册
                ecjvServiceBookConfigDInt = 12;
            }
            if(ecjvServiceBookConfigDInt == 4){   //4	故障诊断手册   =====    11：AMT故障诊断手册
                ecjvServiceBookConfigDInt = 11;
            }
            map.put("ecjvServiceBookConfigDInt",ecjvServiceBookConfigDInt);
            ParameterTool.writeSuccessResponse(response,ecjvServiceBookDao.getECJVServiceBookCatalogue(map));
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }


    /**
     * 根据机型,服务手册类型和目录主键id获取服务手册文件
     * @param map
     * @return
     */
    public ModelAndView getECJVServiceBookList(Map map) {
        HttpServletRequest request = (HttpServletRequest) map.get("request");
        HttpServletResponse response = (HttpServletResponse) map.get("response");
        try {
            // 1.数据校验
            //ecjv机型字典项的DInt值
            int ecjvEngineTypeDInt = MapUtils.getIntValue(map,"ecjvEngineTypeDInt",-1);
            //ecjv服务手册类型列表字典项的DInt值（维护操作手册 维修手册 故障诊断手册使用该字段查询数据,电路图和工具清单没有目录）
            /*1	维护操作手册
            2	维修手册
            3	电路图
            4	故障诊断手册
            5	工具清单*/
            int ecjvServiceBookConfigDInt = MapUtils.getIntValue(map,"ecjvServiceBookConfigDInt",-1);
            //ecjv服务手册类型列表字典项的主键id（电路图 工具清单使用该字段查询数据）
            long dicID = MapUtils.getLongValue(map,"dicID",-1);
            if(ecjvEngineTypeDInt == -1 || dicID == -1 ||
            (ecjvServiceBookConfigDInt != 1 && ecjvServiceBookConfigDInt != 2 && ecjvServiceBookConfigDInt != 3 && ecjvServiceBookConfigDInt != 4 && ecjvServiceBookConfigDInt != 5)){
                ParameterTool.writeErrorResponse(response,-5,"缺少必要参数或所传参数非枚举值！");
                return null;
            }
            //ECJV_Handbook_T表映射值是（11：AMT故障诊断手册     12：AMT维修手册    13：维护操作手册）
            if(ecjvServiceBookConfigDInt == 1){   //1	维护操作手册   =====    13：维护操作手册
                ecjvServiceBookConfigDInt = 13;
            }
            if(ecjvServiceBookConfigDInt == 2){   //2	维修手册   =====    12：AMT维修手册
                ecjvServiceBookConfigDInt = 12;
            }
            if(ecjvServiceBookConfigDInt == 4){   //4	故障诊断手册   =====    11：AMT故障诊断手册
                ecjvServiceBookConfigDInt = 11;
            }
            if(ecjvServiceBookConfigDInt == 3){   //3	电路图   =====    1：电路图
                ecjvServiceBookConfigDInt = 1;
            }
            if(ecjvServiceBookConfigDInt == 5){   //5	工具清单   =====    2：工具清单
                ecjvServiceBookConfigDInt = 2;
            }

            map.put("ecjvServiceBookConfigDInt",ecjvServiceBookConfigDInt);
            List<Map<String,Object>> ecjvServiceBookList = new ArrayList<>();
            //11：AMT故障诊断手册     12：AMT维修手册    13：维护操作手册  查ECJV_HandbookDetail_T表数据
            if(ecjvServiceBookConfigDInt == 13 || ecjvServiceBookConfigDInt == 12 || ecjvServiceBookConfigDInt == 11){
                long bookDirid = MapUtils.getLongValue(map,"bookDirid",-1);
                if(bookDirid == -1){
                    ParameterTool.writeErrorResponse(response,-5,"缺少必要参数或所传参数非枚举值！");
                    return null;
                }
                ecjvServiceBookList = ecjvServiceBookDao.getECJVServiceBookList1(map);
            }else{   //1：电路图   2：工具清单  查ECJV_CircuitDiagram_T表数据
                ecjvServiceBookList = ecjvServiceBookDao.getECJVServiceBookList2(map);
            }
            ParameterTool.writeSuccessResponse(response,ecjvServiceBookList);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }



    /**
     * ecjv更新服务手册阅读次数接口
     * @param map
     * @return
     */
    public ModelAndView ecjvUploadHandTolNum(Map map) {
        HttpServletRequest request = (HttpServletRequest) map.get("request");
        HttpServletResponse response = (HttpServletResponse) map.get("response");
        try {
            // 1.数据校验
            //pdf文件主键id
            long serviceBookId = MapUtils.getLongValue(map,"serviceBookId",-1);
            //服务手册类型：1：维护操作手册 维修手册 故障诊断手册    2： 电路图 工具清单
            int type = MapUtils.getIntValue(map,"type",-1);
            if(serviceBookId == -1 || (type != 1 && type != 2)){
                ParameterTool.writeErrorResponse(response,-5,"缺少必要参数或所传参数非枚举值！");
                return null;
            }
            String tableName = "";
            if(type == 1){  //1：维护操作手册 维修手册 故障诊断手册
                tableName = "ECJV_HandbookDetail_T";
            }else{  //2： 电路图 工具清单
                tableName = "ECJV_CircuitDiagram_T";
            }
            int num = 0;
            if(StringUtils.isNotEmpty(tableName)){
                num = ecjvServiceBookDao.updateHandTolNum(tableName,serviceBookId);
            }
            ParameterTool.writeSuccessResponse(response,num);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

}
